﻿using System.ComponentModel.Composition;
using System.Linq;
using insa.rtos.dsl; // My DSL
using insa.rtos.dsl.ExtensionEnablement; // My DSL
using Microsoft.VisualStudio.Modeling; // Transactions
using Microsoft.VisualStudio.Modeling.Diagrams;
using Microsoft.VisualStudio.Modeling.Diagrams.ExtensionEnablement; // IVsSelectionContext
using Microsoft.VisualStudio.Modeling.ExtensionEnablement; // ICommandExtension
using BuildSourceCodeData;
using System.Collections.Generic;

namespace MyMefExtension
{
    // Defined in Dsl\MefExtension\DesignerExtensionMetaDataAttribute.cs:
    [RTOSDslCommandExtension]
    public class LoadCommandClass : ICommandExtension
    {
        /// <summary>
        /// Provides access to current document and selection.
        /// </summary>
        [Import]
        IVsSelectionContext SelectionContext { get; set; }


        /// <summary>
        /// Called when the user selects this command.
        /// </summary>
        /// <param name="command"></param>
        public void Execute(IMenuCommand command)
        {
            RTOSModelDiagram diagram = SelectionContext.DiagramClientView.Diagram as RTOSModelDiagram;
            RtosModel model = diagram.ModelElement as RtosModel;
                
             //Transaction is required if you want to update elements.
            using (Transaction t = SelectionContext.CurrentStore.TransactionManager.BeginTransaction("Load Model"))
            {                                
                List<ClassModel> modules = Program.LoadModel();
                foreach (ClassModel cm in modules)
                {
                    Service serv = new Service(model.Partition);
                    serv.RtosModel = model;
                    serv.Name = cm.name;
                    
                    foreach (OperationModel om in cm.operations)
                    {
                        Operation op = new Operation(serv.Partition);
                        op.Name = om.name;
                        op.Service = serv;
                    }

                    //foreach (ServiceShape shape in SelectionContext.CurrentSelection)
                    //{
                    //    ServiceShape element = shape.ModelElement as ServiceShape;
                    //    Compartment comp = element.FindCompartment("OperationCompartment");                        
                    //}
                }
                t.Commit();
            }
            //RtosModel model = SelectionContext.CurrentStore. as RTOSModelDiagram;              
        }

        /// <summary>
        /// Called when the user right-clicks the diagram.
        /// Determines whether the command should appear.
        /// This method should set command.Enabled and command.Visible.
        /// </summary>
        /// <param name="command"></param>
        public void QueryStatus(IMenuCommand command)
        {
            //command.Enabled = command.Visible = (SelectionContext.CurrentSelection.OfType<ServiceShape>().Count() > 0);
        }

        /// <summary>
        /// Called when the user right-clicks the diagram.
        /// Determines the text of the command in the menu.
        /// </summary>
        public string Text
        {
            get { return "Load command"; }
        }
    }
}